package cn.eastseven.netserver;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SimpleHandler extends IoHandlerAdapter {

	private static final Logger log = LoggerFactory.getLogger(SimpleHandler.class);
	
	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		super.messageReceived(session, message);
		log.debug("messageReceived: sessionId=[{}], msg=[{}], msgLen=[{}]", session.getId(), message, message.toString().length());
		if("q".equals(message.toString().trim())) {
			log.debug("send OK to client...");
			session.write("OK");
		}
	}
	
	@Override
	public void messageSent(IoSession session, Object message) throws Exception {
		super.messageSent(session, message);
		log.debug("messageSent: sessionId={}, msg=[{}]", session.getId(), message);
	}
	
	@Override
	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
		log.debug("sessionClosed: sessionId={}", session.getId());
	}
	
	@Override
	public void sessionCreated(IoSession session) throws Exception {
		super.sessionCreated(session);
		log.debug("sessionCreated: sessionId={}", session.getId());
	}
	
	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		super.sessionIdle(session, status);
		log.debug("sessionIdle: sessionId={}, status={}", session.getId(), status);
	}
	
	@Override
	public void sessionOpened(IoSession session) throws Exception {
		super.sessionOpened(session);
		log.debug("sessionOpened: sessionId={}", session.getId());
	}
	
	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		super.exceptionCaught(session, cause);
	}
}
